import Cookies from "js-cookie";
import { deepClone } from "@/utils/index.js";
let serviceEmptyBody = {
  inParamsList: [],
  versionId: "",
  svc: {
    appId:"",
  },
  ifParam: {
    adptIfCfgDb: {},
    adptIfCfgHttp: {},
    adptIfCfgJar: {},
  },
  outParamsList: [],
  ifType: "",
};

const state = {
  sidebar: {
    opened: Cookies.get("sidebarStatus")
      ? !!+Cookies.get("sidebarStatus")
      : true,
    withoutAnimation: false,
  },
  device: "desktop",
  size: Cookies.get("size") || "medium",
  scrOpitonList: [],
  serviceBody: {
    inParamsList: [],
    versionId: "",
    svc: {
      appId:"",
      wrapped:true
    },
    ifParam: {
      adptIfCfgDb: {},
      adptIfCfgHttp: {},
      adptIfCfgJar: {},
    },
    outParamsList: [],
    ifType: "",
    
  },
};

const mutations = {
  // 服务新增数据
  PUT_SERVICE_ALL: (state, plyload) => {
    state.serviceBody = plyload;
  },
  PUT_SERVICE_CLAER: (state, plyload) => {
    state.serviceBody = deepClone(serviceEmptyBody);
  },
  PUT_SERVICE_VERSION: (state, plyload) => {
    state.serviceBody.versionId = plyload;
  },
  PUT_SERVICE_CREAT: (state, plyload) => {
    state.serviceBody.versionId = "";
    state.serviceBody.ifParam = {
      adptIfCfgHttp: {
        inParamsJson: { header: [], queryParam: [], body: [] },
      },
      adptIfCfgDb: { inParamsJson: [], sqlStmt: "" },
      adptIfCfgJar: {},
    };
    state.serviceBody.ifType = "";
    state.serviceBody.inParamsList = [];
    state.serviceBody.outParamsList = [];
  },
  PUT_SERVICE_1: (state, plyload) => {
    state.serviceBody.svc = plyload;
  },
  PUT_SERVICE_2: (state, plyload) => {
    state.serviceBody.inParamsList = plyload;
  },
  PUT_SERVICE_3: (state, plyload) => {
    state.serviceBody.ifType = plyload;
  },
  PUT_SERVICE_DOC: (state, plyload) => {
    state.serviceBody.doc = plyload;
  },
  PUT_SERVICE_HTTP: (state, plyload) => {
    state.serviceBody.ifParam.adptIfCfgHttp = plyload;
  },
  PUT_SERVICE_DB: (state, plyload) => {
    state.serviceBody.ifParam.adptIfCfgDb = plyload;
  },
  PUT_SERVICE_JAR: (state, plyload) => {
    state.serviceBody.ifParam.adptIfCfgJar = plyload;
  },
  // 出参标准化
  PUT_SERVICE_OUT_PARAMS: (state, plyload) => {
    state.serviceBody.outParamsList = plyload;
  },
  PUT_SERVICE_OUT_FORMATLIST: (state, plyload) => {
    state.serviceBody.outputFormatList = plyload;
  },
  PUT_SERVICE_HTTP_AUTH: (state, plyload) => {
    state.serviceBody.ifParam.adptIfCfgHttp.authCfgJson = plyload;
  },
  // 新增保存ID
  PUT_SERVICE_ID: (state, plyload) => {
    state.serviceBody.svc.svcId = plyload;
  },

  // 添加数据源
  ADD_SRC_OPTION: (state, plyload) => {
    state.scrOpitonList = plyload;
  },
  TOGGLE_SIDEBAR: (state) => {
    state.sidebar.opened = !state.sidebar.opened;
    state.sidebar.withoutAnimation = false;
    if (state.sidebar.opened) {
      Cookies.set("sidebarStatus", 1);
    } else {
      Cookies.set("sidebarStatus", 0);
    }
  },
  CLOSE_SIDEBAR: (state, withoutAnimation) => {
    Cookies.set("sidebarStatus", 0);
    state.sidebar.opened = false;
    state.sidebar.withoutAnimation = withoutAnimation;
  },
  TOGGLE_DEVICE: (state, device) => {
    state.device = device;
  },
  SET_SIZE: (state, size) => {
    state.size = size;
    Cookies.set("size", size);
  },
};

const actions = {
  toggleSideBar({ commit }) {
    commit("TOGGLE_SIDEBAR");
  },
  closeSideBar({ commit }, { withoutAnimation }) {
    commit("CLOSE_SIDEBAR", withoutAnimation);
  },
  toggleDevice({ commit }, device) {
    commit("TOGGLE_DEVICE", device);
  },
  setSize({ commit }, size) {
    commit("SET_SIZE", size);
  },
  putService({ commit }, plyload) {
    commit("PUT_SERVICE", plyload);
  },
};

export default {
  namespaced: true,
  state,
  mutations,
  actions,
};
